package connectDatabase;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

/**
 * Servlet implementation class for Servlet: ConnectMSSQL
 * 
 */
public class ConnectMSSQL extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ConnectMSSQL() {
		super();
	}

	private void processResponse(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		try {
			//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			SQLServerDataSource ds = new SQLServerDataSource();
			ds.setUser("sa");
			ds.setPassword("toilatoi");
			ds.setServerName("localhost");
			ds.setPortNumber(1433); 
			ds.setDatabaseName("QLKS");
			Connection connect = ds.getConnection();
			Statement statement = connect.createStatement();
			ResultSet resultSet = statement
					.executeQuery("select * from NHANVIEN");
			while (resultSet.next()) {
				printWriter.print("Ho ten nhan vien : "
						+ resultSet.getString("HOTENNV") + "<br /> Mat khau : "
						+ resultSet.getString("MatKhau") + "<br />");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.print("\n\n\n\n\n\n\nDien qua' di thui T_T\n\n\n\n\n\n\n\n\n\n");
		}

	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processResponse(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.processResponse(request, response);
	}
}